﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ListForms;

namespace ListsForm
{
    public partial class FrmCompany : FrmBaseListForm
    {
        public FrmCompany()
        {
            InitializeComponent();
        }

        public override void InitGridColumns()
        {
            DevExpress.XtraGrid.Columns.GridColumn colID = new DevExpress.XtraGrid.Columns.GridColumn();
            colID.Caption = "ID";
            colID.FieldName = "CompanyNo";
            colID.Name = "colID";
            colID.Visible = true;
            colID.Width = 100;
            colID.MinWidth = 100;
            colID.OptionsColumn.FixedWidth = true;
            colID.VisibleIndex = 0;

            DevExpress.XtraGrid.Columns.GridColumn colName = new DevExpress.XtraGrid.Columns.GridColumn();
            colName.Caption = "Name";
            colName.FieldName = "CompanyName";
            colName.Name = "colName";
            colName.Visible = true;
            colName.Width = 100;
            colName.MinWidth = 100;
            colName.OptionsColumn.FixedWidth = true;
            colName.VisibleIndex = 1;

            DevExpress.XtraGrid.Columns.GridColumn colAddress = new DevExpress.XtraGrid.Columns.GridColumn();
            colAddress.Caption = "Address";
            colAddress.FieldName = "CompanyAddress";
            colAddress.Name = "colAddress";
            colAddress.Visible = true;
            colAddress.Width = 100;
            colAddress.MinWidth = 100;
            colAddress.OptionsColumn.FixedWidth = true;
            colAddress.VisibleIndex = 2;

            DevExpress.XtraGrid.Columns.GridColumn colPhoneNumber = new DevExpress.XtraGrid.Columns.GridColumn();
            colPhoneNumber.Caption = "PhoneNumber";
            colPhoneNumber.FieldName = "CompanyTel";
            colPhoneNumber.Name = "colAddress";
            colPhoneNumber.Visible = true;
            colPhoneNumber.Width = 100;
            colPhoneNumber.MinWidth = 100;
            colPhoneNumber.OptionsColumn.FixedWidth = true;
            colPhoneNumber.VisibleIndex = 3;

            this.gridViewDsDoiTuong.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] {
                colID, colName, colAddress, colPhoneNumber});

            foreach (DevExpress.XtraGrid.Columns.GridColumn gc in gridViewDsDoiTuong.Columns)
            {
                gc.FilterMode = DevExpress.XtraGrid.ColumnFilterMode.DisplayText;
                gc.OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains;
            }
        }

        public override void LoadData()
        {
            try
            {
                DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.Companies);
                gridDSDoiTuong.DataSource = DB.Companies.ToList();
            }
            catch (Exception ex)
            {
                BasicForms.FrmWarning.Show(ex.Message);
            }
        }

        public override void CreateNew()
        {
            try
            {
                FrmEditCompany frm = new FrmEditCompany(null);
                frm.RequestChangeInstance += new FrmBaseEditForm.OnRequestChangeInstance(LoadData);
                frm.ShowDialog();
            }
            catch (Exception ex)
            {
                BasicForms.FrmWarning.Show(ex.Message);
            }
        }

        protected override void Edit()
        {
            try
            {
                int[] tmp = gridViewDsDoiTuong.GetSelectedRows();

                if (tmp != null && tmp.Length > 0)
                {
                    int selectedRowIndex = tmp[0];
                    Lists.Company company = (Lists.Company)gridViewDsDoiTuong.GetRow(selectedRowIndex);
                    if (company != null)
                    {
                        FrmEditCompany frm = new FrmEditCompany(company);
                        frm.RequestChangeInstance += new FrmBaseEditForm.OnRequestChangeInstance(LoadData);
                        frm.ShowDialog();
                    }
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmWarning.Show(ex.Message);
            }
        }

        protected override void Delete()
        {
            try
            {
                int[] selectedIndexes = gridViewDsDoiTuong.GetSelectedRows();

                if (selectedIndexes != null && selectedIndexes.Length > 0)
                {
                    int selectedRowIndex = selectedIndexes[0];
                    Lists.Company deletedObj = (Lists.Company)gridViewDsDoiTuong.GetRow(selectedRowIndex);
                    if (deletedObj != null)
                    {
                        if (MessageBox.Show("Are you sure delete " + "'" + deletedObj.CompanyName + "'" + "?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.No)
                            return;
                        else
                        {
                            DB.Companies.DeleteOnSubmit(deletedObj);
                            DB.SubmitChanges();
                            LoadData();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                BasicForms.FrmWarning.Show("Cannot delete this company!");
            }
        }
    }
}
